<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>散客买茶第二步</title>
<jsp:include page="/header.jsp"></jsp:include>
<link href="${basePath }/css/ui.css" rel="stylesheet" type="text/css">
<script type="text/javascript">
	$(function() {
		
		loadCookie();
		
		$("#oldcustomer_dialog").dialog({
			title:"客户信息",
			autoOpen: false,
			height: 300,
			width: 500,
			modal: true,
		});
		
		$("#newcustomer_dialog").dialog({
			title:"添加新客户",
			autoOpen: false,
			height: 400,
			width: 300,
			modal: true,
			buttons:[{
				text:"确定",
				click:function() {
					$.ajax({
						url:"${basePath}/scat/addCustomer.htm",
						type:"post",
						data:$("#newcustomer_form").serialize(),
						dataType:"json",
						success:function(data) {
							$.jqalert(data.msg, "提示", function(){
								if(data.isSuccess) {
									$("#newcustomer_dialog").dialog("close");
									var id = data.json.id;
									$("#newcustomer_table").find("input[name='id']").val(id);
									confirm("newcustomer");
									$("#newcustomer_form").find("input").val("");
								}
							});
						}
					});
				}
			},{
				text:"清空",
				click:function() {
					$("#newcustomer_form").find("input").val("");
				}
			}]
		});
	});
	
	function loadCookie() {
		var products = eval("("+$.cookie("products")+")");
		var price = 0;
		$.each(products, function(i, n){
			var html = "<tr><td><input type='checkbox' onclick='check(this);'></td><td name='name'>"+n.name+"</td><td name='code'>"+n.code+
				"</td><td name='productionyear'>"+n.productionyear+"</td><td name='batch'>"+n.batch+"</td><td name='type'>"+n.type+"</td><td name='price'>"+
				n.price+"</td><td name='discount'>"+n.discount+"</td><td name='price2'>"+n.price2+"</td>"+
				"<input type='hidden' name='product_id' value='"+n.product_id+"'>"+
				"<input type='hidden' name='unit_type' value='"+n.unit_type+"'></tr>";
			$("#buy").append(html);
			price += Number(n.price2);
		});
		$("#buy_price").text(price);
	}
	
	//勾选复选框
	function check(ck) {
		var discount = "${saveDiscount.value}";
		var n = $(ck).parent().parent();
		if(ck.checked) {
			var size = $("#check").find("tr").size();
			$("#check").append("<tr>"+n.html()+"</tr>");
			var tr = $("#check").find("tr").last();
			var tds = tr.find("td");
			tr.attr("id", tds.eq(2).text());
			tds.eq(0).text(size);
			tds.eq(6).text(tds.eq(8).text());
			tds.eq(7).text(Number(discount));
			var price = Number(tds.eq(8).text());
			tds.last().text(price.mul(Number(discount)));
		} else {
			var id = n.find("td").eq(2).text();
			$("#"+id).remove();
		}	
		
		changeSavePrice();
	}
	
	//老客户弹窗
	function oldcustomer() {
		$("#oldcustomer_dialog").dialog("open");
	}
	
	//新客户弹窗
	function newcustomer() {
		$("#newcustomer_dialog").dialog("open");
	}
	
	//查询老客户
	function findcustomer() {
		var name = $("#oldcustomer_form").find("input[name='name']").val();
		var tel = $("#oldcustomer_form").find("input[name='tel']").val();
		if(name==""&&tel=="") $.jqalert("请输入查询条件","提示");
		else {
			$.ajax({
				url:"${basePath}/scat/findcustomer.htm",
				type:"post",
				data:$("#oldcustomer_form").serialize(),
				dataType:"json",
				success:function(data) {
					var html = "";
					$.each(data, function(i, n){
						if(i==0) {
							html += "<tr>"+
							"<th>序号</th><th>姓名</th><th>电话</th>"+
							"</tr>";
						}
						html +=
							"<tr><td><input type='radio' name='customer'>"+
						"<input type='hidden' name='fax' value='"+fkong(n.fax)+"'>"+
						"<input type='hidden' name='email' value='"+fkong(n.email)+"'>"+
						"<input type='hidden' name='comaddress' value='"+fkong(n.comaddress)+"'>"+
						"<input type='hidden' name='address' value='"+fkong(n.address)+"'>"+
						"<input type='hidden' name='name' value='"+fkong(n.name)+"'>"+
						"<input type='hidden' name='telphone' value='"+fkong(n.telphone)+"'>"+
						"<input type='hidden' name='id' value='"+fkong(n.id)+"'>"+
						"</td><td>"
						+fkong(n.name)+"</td><td>"+fkong(n.telphone)+"</td></tr>";
					});
					$("#oldcustomer_table").html(html);
				}
			});
		}
	}
	
	//读出客户信息
	function confirm(tableId) {
		var table = $("#"+tableId+"_table");
		var final_table = $("#final_customer_table");
			final_table.parent().show();
			var inputs = {};
			if(tableId=="newcustomer") {
				inputs = table.find("input");
			} else if(tableId=="oldcustomer") {
				inputs = table.find("[type='radio']:checked").parent().parent().find("input");
			}
			$.each(inputs, function(i, n){
				var name = $(n).attr("name");
				if(name=="id") final_table.find("[name='id']").val($(n).val());
				else final_table.find("[name='"+name+"']").text($(n).val());
			});
			var dialog = $("#"+tableId+"_dialog");
			dialog.dialog("close");
			
			if(tableId=="oldcustomer") {
				table.find("tr:gt(0)").remove();
				dialog.find("input[type='text']").val("");
			}
	}
	
	//去结算
	function buy(a) {
		var order = {};
		order["type"] = 1;
		order["sum_buy"] = $("#buy_price").text();
		order["sum_save"] = $("#save_price").text();
		
		var saves = new Array();
		$.each($("#check").find("tr:gt(0)"),function(i,n) {
			var save = {};
			$.each($(n).find("td:gt(0)"),function(i,n){
				eval("save."+$(n).attr("name")+"=\""+$(n).text()+"\"");
			});
			//save.id = $(n).find("input[type='hidden']").val();
			save.is_save = 1;
			save.product_id = $(n).find("input[name='product_id']").val();
			save.unit_type = $(n).find("input[name='unit_type']").val();
			saves.push(save);
		});
		
		var customer_div = $("#final_customer_table");
		var id = customer_div.find("[name='id']").val();
		var name = customer_div.find("[name='name']").text();
		var customer = {};
		customer.id = id;
		customer.name = name;
		
		$.cookie("order", JSON.stringify(order), {path:"/"});
		$.cookie("saves", JSON.stringify(saves), {path:"/"});
		$.cookie("customer", JSON.stringify(customer), {path:"/"});
		
		window.location = "${basePath}/scat/pay.htm";
	}
	
	function viewCookie() {
		alert($.cookie("products"));
	}
	
	function changeSavePrice() {
		var sum = 0;
		$.each($("#check").find("tr:gt(0)"),function(i,n) {
			sum = sum.add(Number($(n).find("td").eq(8).text()));
		});
		$("#save_price").text(sum);
	}
	
	function test() {
		
	}
	
	function back() {
		/* var back_discounts = new Array();
		var trs = $("#buy").find("tr:gt(0)");
		$.each(trs,function(i,n) {
			var back_discount = {};
			var code = $(n).find("td").eq(2).text();
			var discount = $(n).find("td").eq(7).text();
			eval("back_discount.key=\""+code+"\"");
			eval("back_discount.value=\""+discount+"\"");
			//suffix += code+"="+discount;
			//if(i<trs.size()-1) suffix += "&";
			back_discounts.push(back_discount);
		});
		$.cookie("back_discounts", JSON.stringify(back_discounts), {path:"/"}) */;
		window.location = "${basePath }/scat/index.htm";
	}
</script>
</head>
<body>
    <div class="main">
        <div class="cont">
            <h2 class="title">结算的商品：</h2>
            <div class="ui">
				<table id="buy" class="table_list">
					<tr class="">
						<th class="">是否寄存</th>
						<th>品名</th>
						<th class="">二维码</th>
						<th class="">年份</th>
						<th class="">批次</th>
						<th class="">类型</th>
						<th>价格(元)</th>
						<th class="">折扣</th>
						<th class="">折后价</th>
					</tr>
				</table>
				<span class="sel_label fr">结算费用：<span id="buy_price">0</span>元</span>
            </div>
            
            <h2 class="title">寄存的商品：</h2>
            <div class="ui">
                <table id="check" class="table_list">
                    <tr class="">
                      <th class="">序号</th>
                      <th>品名</th>
                      <th class="">二维码</th>
                      <th class="">年份</th>
                      <th class="">批次</th>
                      <th class="">类型</th>
                      <th>折后价格</th>
                      <th class="">折扣</th>
                      <th class="">寄存费用</th>
                    </tr>
                </table>
                <span class="sel_label fr">寄存合计：<span id="save_price">0</span>元</span>
            </div>
            
            <h2 class="title">寄存者信息：
	            <a href="javascript:;" onclick="oldcustomer();">老客户</a>
	            <a href="javascript:;" onclick="newcustomer();">新客户</a>
            </h2>
            
	        <div id="oldcustomer_dialog" style="font-size:10px;">
				<form id="oldcustomer_form">
				姓名：<input type="text" name="name">&nbsp;手机号：<input type="text" name="tel">
				<input type="button" value="查找" onclick="findcustomer();">
				<input type="button" value="确定" onclick='confirm("oldcustomer");'></form><hr>
				<table id="oldcustomer_table" style="width:100%">
					<tr>
						<th>序号</th><th>姓名</th><th>电话</th>
					</tr>
				</table>
			</div>
			<div id="newcustomer_dialog" style="font-size:10px;">
				<form id="newcustomer_form">
				<table id="newcustomer_table">
					<tr>
						<th>客户类别：</th>
						<td>
							<select name="sort">
							<option value="">--请选择--</option>
							<c:forEach items="${customertypes }" var="type">
								<option value="${type.code }">${type.desc }</option>
							</c:forEach>	
							</select>
						</td>
					</tr>
					<tr><th>客户姓名：</th><td><input type="text" name="name"></td></tr>
					<tr><th>联系电话：</th><td><input type="text" name="telphone"></td></tr>
					<tr><th>传真：</th><td><input type="text" name="fax"></td></tr>
					<tr><th>邮箱：</th><td><input type="text" name="email"></td></tr>
					<tr><th>公司地址：</th><td><input type="text" name="comaddress"></td></tr>
					<tr><th>送货地址：</th><td><input type="text" name="address"></td></tr>
					<input type="hidden" name="id">
				</table>
				</form>
			</div>
			<div class="b_div" style="display:none;">
				<div id="final_customer_table">
					<input type="hidden" name="id">
					<p class="clearfix">
						<span class="info_s fl">客户名称：<span name="name"></span></span>
						<span class="info_s fl">联系电话：<span name="telphone"></span></span>
						<span class="info_s fl">传真：<span name="fax"></span></span>
						<span class="info_s fl">邮箱：<span name="email"></span></span>
					</p>
					<p class="clearfix">
						<span class="info_s fl">公司地址：<span name="comaddress"></span></span>
					</p>
					<p class="clearfix">
						<span class="info_s fl">送货地址：<span name="address"></span></span>
					</p>
				</div>
			</div>
			
			<div class="text_c">
	        	<a href="javascript:;" class="yellow_btn btn" onclick="buy();">现在去结账</a>
	        	&nbsp;&nbsp;
	        	<a href="javascript:;" onclick="back();">返回上一步</a>
            
        </div>
    </div>
</body>
</html>